package com.example.cnotebook.util;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpUtil {

    //    private static final String baseUrl = "http://192.168.4.2:8080";
    private static final String baseUrl = "http://192.168.0.110:8080";

    public static void get(String url, final CallBack callBack, final Activity activity) {
        url = baseUrl + url;
        System.out.println("请求地址==============》》》》" + url);
        Request.Builder requestUrl = new Request.Builder().url(url);
        requestUrl.method("GET", null);
        Request build = requestUrl.build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String string = response.body().string();
                System.out.println("服务器返回==============》》》》" + string);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            callBack.onSuccess(new JSONObject(string));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    public static void post(String url, JSONObject requestParams, final CallBack callBack, final Activity activity) {
        url = baseUrl + url;
        System.out.println("请求地址==============》》》》" + url);
        MediaType type = MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody = RequestBody.create(requestParams.toString(), type);
        Request build = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                System.out.println("服务器返回==============》》》》" + string);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            callBack.onSuccess(new JSONObject(string));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    public static void uploadFile(String url, List<File> files, final CallBack callBack, final Activity activity) {
        url = baseUrl + url;
        System.out.println("请求地址==============》》》》" + url);
        MediaType type = MediaType.parse("multipart/form-data; charset=utf-8");
        for (int i = 0; i < files.size(); i++) {
            if (!files.get(i).exists()) {
                System.out.println("当前文件不存在：" + files.get(i));
                return;
            }
        }
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (int i = 0; i < files.size(); i++) {
            builder.addFormDataPart("file", files.get(i).getName(), RequestBody.create(type, files.get(i)));
        }
        RequestBody requestBody = builder.build();
        Request build = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                System.out.println("服务器返回==============》》》》" + string);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            callBack.onSuccess(new JSONObject(string));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    public static void downLoadAudio(String url, final CallBackDownLoad callBack) {
        url = baseUrl + url;
        System.out.println("请求地址==============》》》》" + url);
        RequestBody body = RequestBody.create(null, "");
        Request build = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    callBack.onSuccess(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public interface CallBack {
        void onSuccess(JSONObject response) throws JSONException;

        void onFailure(int errorNo, String strMsg);
    }

    public interface CallBackDownLoad {
        void onSuccess(Response response) throws JSONException;
    }
}
